package com.zlzkj.app.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RequestParam;
// import org.apache.poi.hssf.usermodel.HSSFWorkbook;
////import org.apache.shiro.SecurityUtils;
////import org.apache.shiro.authz.annotation.RequiresRoles;
////import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.multipart.MultipartFile;
//import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

//import com.alibaba.fastjson.JSONObject;
import com.zlzkj.core.base.BaseController;
//import com.zlzkj.core.sql.Row;
import com.zlzkj.app.model.Energymac;
import com.zlzkj.app.service.EnergytypeService;
//import com.zlzkj.app.service.RoleService;
//import com.zlzkj.app.service.ShiroUserService;
import com.zlzkj.app.service.EnergymacService;
import com.zlzkj.app.util.DownloadUtil;
import com.zlzkj.app.util.MapUtil;
import com.zlzkj.app.util.PageView;
import com.zlzkj.app.aop.Log;

@Controller
@RequestMapping(value = "/energymac")

public class EnergymacController extends BaseController {

    @Value("${PAGE_SIZE}")
    private int PAGE_SIZE;

    @Value("${FILE_REPOSITORY}")
    private String FILE_REPOSITORY;

    @Autowired
    private EnergymacService energymacService;

//    @Autowired
//    private RoleService roleService;

//    @Autowired
//    private ShiroUserService shiroUserService;

    @Autowired
    private EnergytypeService energytypeService;

    //@RequiresPermissions(value = "device:list")
    @RequestMapping(value = {"/index"})
    public String index(Model model, HttpServletRequest request, HttpServletResponse response, Integer nowPage) {
        Map map = MapUtil.convert(request.getParameterMap());
        model.addAttribute("pageView", new PageView(energymacService.findByMap(map, nowPage)));
        return "machine/index";
    }

    @Log(name="新增设备能源数据")
    @RequestMapping(value = {"/create"}, method = RequestMethod.POST)
    public String create(Model model, HttpServletRequest request, HttpServletResponse response, Energymac energymac) {
        return ajaxReturn(response, energymacService.save(energymac));
    }
    
    @ResponseBody
    @Log(name="删除设备能源数据")
    @RequestMapping(value = {"/delete"}, method = RequestMethod.POST)
    public String delete(Model model, HttpServletRequest request, HttpServletResponse response, String[] id) {
        return ajaxReturn(response, energymacService.deletebyIds(id));
    }

    @Log(name="改设备能源数据")
    @RequestMapping(value = {"/update"}, method = RequestMethod.POST)
    public String edit(Model model, HttpServletRequest request, HttpServletResponse response, Energymac energymac) {
        return ajaxReturn(response, energymacService.update(energymac));
    }
    
    @ResponseBody
    @Log(name="导出设备能源报表")
    @RequestMapping(value = {"/excel"}, method = RequestMethod.GET)
    public void excel(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {   
        String outFile = "excel.xls";
        DownloadUtil down = new DownloadUtil();
        ByteArrayOutputStream bt = energymacService.getExcel();
        down.download(bt, response, outFile);
    }

    //json 机器列表
    @ResponseBody
    @Log(name="查看设备能源列表")
    @RequestMapping(value = {"/list"}, method = RequestMethod.POST)
    public Map<String, Object> machineList(HttpServletRequest request, Integer nowPage) {
        Map<String, Object> result = new HashMap();
        Map map = MapUtil.convert(request.getParameterMap());
        result.put("pageView", new PageView(energymacService.findByMap(map, nowPage)));
        return result;
    }

    @ResponseBody
    @RequestMapping(value = {"/to_create"}, method = RequestMethod.POST)
    public Map<String, Object> toCreate(HttpServletRequest request) {
        Map<String, Object> result = new HashMap();
        result.put("energy", energytypeService.findByMap(new HashMap<String, Object>()));
        //result.put("roles", roleService.findByMap(new HashMap<String, Object>()));
        return result;
    }

    @ResponseBody
    @RequestMapping(value = {"/to_edit"}, method = RequestMethod.POST)
    public Map<String, Object> toEdit(HttpServletRequest request, String id) {
        Map<String, Object> result = new HashMap();
        result.put("energy", energytypeService.findByMap(new HashMap<String, Object>()));
        //result.put("roles", roleService.findByMap(new HashMap<String, Object>()));
        result.put("obj", energymacService.findById(id));    
        return result;
    }
}
